/*
 * @Author: i@douxuefeng.cn
 * @Date: 2022-04-12 11:32:16
 * @LastEditTime: 2022-04-12 12:44:00
 * @LastEditors: i@douxuefeng.cn
 * @Description:
 * @FilePath: \go-wechat\pay\native\native.go
 */
package native

import (
	"gitee.com/douxuefeng/go-wechat/pay/config"
	"gitee.com/douxuefeng/go-wechat/pay/tools"
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
	pay_notive "github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
)

type native struct {
	*config.PayConfig
}

func NewNative(cfg *config.PayConfig) *native {
	return &native{cfg}
}

func (p *native) PrepayOrder(prepay Prepay) (*pay_notive.PrepayResponse, *core.APIResult, error) {
	client, ctx, err := tools.GetClient(p.PayConfig)
	if err != nil {
		return nil, nil, err
	}
	svc := pay_notive.NativeApiService{Client: client}
	resp, result, err := svc.Prepay(ctx, pay_notive.PrepayRequest{
		Appid:       core.String(p.Appid),
		Mchid:       core.String(p.MchId),
		Description: core.String(prepay.Description),
		OutTradeNo:  core.String(prepay.OutTradeNo),
		Attach:      core.String(prepay.Attach),
		NotifyUrl:   core.String(p.Notify),
		Amount: &pay_notive.Amount{
			Total: core.Int64(prepay.Amount),
		},
	})
	return resp, result, err
}

func (p *native) QueryOrderByOutTradeNo(outTradeNo string) (*payments.Transaction, *core.APIResult, error) {
	client, ctx, err := tools.GetClient(p.PayConfig)
	if err != nil {
		return nil, nil, err
	}
	svc := pay_notive.NativeApiService{Client: client}
	resp, result, err := svc.QueryOrderByOutTradeNo(ctx, pay_notive.QueryOrderByOutTradeNoRequest{
		OutTradeNo: core.String(outTradeNo),
		Mchid:      core.String(p.MchId),
	})
	return resp, result, err
}
